import { createPinia, defineStore } from "pinia";
//创建菠萝
export let Pinia = createPinia();

//数据类型
import type { StoreType , CatType ,UserStoreType} from "../types";

//引入API
import API from '../api/index'
import ClassifySmallView from "../views/ClassifySmallView.vue";


//导出菠萝数据
export  const userBaseStore = defineStore('base',{
    state(): StoreType{
        return{
            //深色模式数据
            isDark: false,
            //轮播图数据
            bannerList: [],
            // 首页分类导航
            nowActive: '0000',
            // 热门商品数据
            hotList:[],
            //分类数据
            catList:[],

            // ClassifySmallList:[],
            
            //热门商品数据
            hotGoodsList:[],

            //分类商品数据
            otherGoodsList:[],
            // 分类导航
            classifyAllList:[],

            

        }
    },

    //计算属性
    getters:{

    },
    //方法
    actions: {
        //请求轮播图数据
        async getBannerData(){
            let even = await API.getData('/api_banner',{bannerId:1});
            this.bannerList = even;
        },
        //分类数据
        async getCatData(){
            let even = await API.getData('/api_cat');
            
            this.hotList = even;
            this.hotList.unshift({
                cat_id: '0000',
				cat_name: '首页',
				cat_img: 'default.png'
            });
            this.catList = even;
          
            
            
        },
        // //分类数据
        // async getCatsmallData(){
        //     let even = await API.getData('/api_classify');
            
        //     this.ClassifySmallList = even;
           
        //     console.log( this.ClassifySmallList)
          
            

        // },
        //热门商品数据
        async getHotGoodsData(){
            this.hotGoodsList = [];
            let cat = await API.getData('/api_cat');
            //遍历拼接
            cat.forEach(async (item:CatType) => {
                this.hotGoodsList.push(
                    {
						cat_id : item.cat_id,
						cat_name: item.cat_name,
						data : await API.getData('/api_goods', {catId: item.cat_id, pagesize:4})
					}
                );
            });

            // console.log(this.hotGoodsList);
            
        },

        //分类商品数据
        async getOtherData(){
            this.otherGoodsList = [];

            if(this.nowActive == '0000') return;
            //请求小分类
            let even = await API.getData('/api_classify');
            
            // console.log(this.nowActive);
            
           

            let arrList = [];

            even.forEach(item=>{
                if(item.cat_id == this.nowActive){
                    arrList = [...item.data];
                }
            })
            // 拼接数据
            arrList.forEach(async item =>{
                let even = await API .getData('/api_search',{keywords:item.product_content});
                if(even.length == 0) return;

                this.otherGoodsList.push({
                    cat_id: this.nowActive,
                    cat_name: item.product_content,
                    data: even
                });
            })

        },

        //小分类数据
        async getClassifyAllData(){
            let even = await API.getData('/api_classify');
            this.classifyAllList = even;
            // console.log(this.classifyAllList)

            return even;
            
            
        },

        
    }
})


// 权限类
export const useUserStore = defineStore('user', {
	state(): UserStoreType {
		return {
			TOKEN: window.localStorage.getItem('token'),
			
            //购物车数据
            userCartList: [],
            //导航条购物车数量
            userCartNumber: 0,
            nowActiveAddressId: 0,
            // 用户地址数据
            userAddressOriginList: [],
            userAddressList:[],
            // 编辑数据
            AddressEditInfo: null,
            // 是否为编辑
            isEditId: 0,
		}
	},

    actions:{
        //获取购物车数据
        async getUserCarData(){
            if(!this.TOKEN){
				console.log('getUserCartData请求失败，未登录');
				return;
			}
            // 请求购物车数据
            let even = await API.getCarData('/api_cart',{
                status: 'viewcart',
				userId: this.TOKEN
            });
            // console.log(even)
            even.forEach((item: Goods) => {
				item.isChecked = false;
			});
            // 购物车数据
            this.userCartList = even;
            // 购物车数量
			this.userCartNumber = even.length;
        },
        // 获取用户地址数据
        async getAddressData(){
            if(!this.TOKEN){
                console.log('getUserCartData请求失败，未登录');
				return;
            };

            let even = await API.getAddressData('/api_address',{
                status : 'getAddress',
                userId: this.TOKEN
            });
            // console.log(even)
            this.userAddressOriginList = even;
            // 整理数据
            this.userAddressList = even.map((item:userAddressList) =>{
                if(item.isActive){
                    this.nowActiveAddressId = item.address_id;
                };
                
                return {
					id: item.address_id,
					name: item.takename,
					tel: item.tel,
					address: item.province + item.city + item.district + item.streetname,
					isDefault: item.isDefault,
					isActive: item.isActive,
				}

            });

            // console.log(this.userAddressList,this.nowActiveAddressId);
            
            

        }
    }
})

